const { ccclass, property } = cc._decorator;
//用于存取节点和组件
@ccclass
export default class UIContainer {
    //存节点容器
    private _mapNode: Map<string, cc.Node> = new Map();

    //存储组件的容器
    private _mapLabel: Map<string, cc.Label> = new Map();
    private _mapSprite: Map<string, cc.Sprite> = new Map();
    private _mapProgressBar: Map<string, cc.ProgressBar> = new Map();
    private _mapPageView: Map<string, cc.PageView> = new Map();
    private _mapScrollView: Map<string, cc.ScrollView> = new Map();
    private _mapSlider: Map<string, cc.Slider> = new Map();
    private _mapToggle: Map<string, cc.Toggle> = new Map();

    private _objCompMap = {
        Label: this._mapLabel,
        Sprite: this._mapSprite,
        ProgressBar: this._mapProgressBar,
        PageView: this._mapPageView,
        ScrollView: this._mapScrollView,
        Slider: this._mapSlider,
        Toggle: this._mapToggle
    }

    //存储需要关注的节点的接口
    addNode(nodeName: string, node: cc.Node) {
        if (nodeName.length <= 0 || this._mapNode.has(nodeName) || !node) {
            cc.error("参数没有传递正确或此节点已经存储");
            return;
        }

        this._mapNode.set(nodeName, node);
    }


    //获取需要关注的节点的接口
    getNode(nodeName: string) {
        if (nodeName.length <= 0 || !this._mapNode.has(nodeName)) {
            cc.error("参数没有传递正确或不存在此节点");
            return null;
        }

        return this._mapNode.get(nodeName);
    }



    //存储需要关注的组件
    addComp(nodeName: string, compName: string, comp: cc.Component) {
        if (nodeName.length <= 0 || compName.length <= 0 || !comp) {
            cc.error("参数没有传递正确");
            return;
        }
        let compMap = this._objCompMap[compName];
        if (!compMap) {
            cc.error("不存在存储" + compName + "组件的容器");
            return;
        }
        //存储时存储节点名和组件
        compMap.set(nodeName, comp);
    }

    //获取需要关注的组件
    getComp<T>(nodeName: string, compName: string): T {
        if (nodeName.length <= 0 || compName.length <= 0) {
            return null;
        }

        for (let comp in this._objCompMap) {
            if (compName == comp) {
                let compMap = this._objCompMap[compName];
                if (!compMap) {
                    cc.error("不存在存储" + compName + "组件的容器");
                    return null;
                }
                return compMap.get(nodeName) as T;
            }
        }
    }




}


